// MyClass.cs created with MonoDevelop
// User: gaetan at 22:13 13/03/2009
//


using System;
//using GConf;
//use xml for don't use Gconf (now can work with windows ;)) 
using System.Xml;
using System.IO;
using Metier;

namespace Gconf
{
	
	
	public class Conf
	{
		
		private string dirtwitter=Environment.GetEnvironmentVariable("HOME")+"/.microblog/";
		private string _NameTwitter;
		
		public string NameTwitter {
			get { return _NameTwitter; }
			set { _NameTwitter = value; }
		}
		private string _PassTwitter;
		
		public string PassTwitter {
			get { return _PassTwitter; }
			set { _PassTwitter = value; }
		}
		private string _NameIdenti;
		
		public string NameIdenti {
			get { return _NameIdenti; }
			set { _NameIdenti = value; }
		}
		private string _PassIdenti;
		
		public string PassIdenti {
			get { return _PassIdenti; }
			set { _PassIdenti = value; }
		}
		private string _Time;
		
		public string Time {
			get { return _Time; }
			set { _Time = value; }
		}
		
		public Boolean SaveConfig()
		{
			try{	
				
				//création répertoire
				if (!Directory.Exists(dirtwitter)){
					Directory.CreateDirectory(dirtwitter);
				}
				XmlTextWriter myXmlTextWriter = new XmlTextWriter (dirtwitter+"config.xml", System.Text.Encoding.UTF8);
				myXmlTextWriter.Formatting = Formatting.Indented;
				myXmlTextWriter.WriteStartDocument(true);
				myXmlTextWriter.WriteStartElement("Config");
				myXmlTextWriter.WriteAttributeString("NameTwitter", NameTwitter);
				myXmlTextWriter.WriteAttributeString("PassTwitter", PassTwitter);
				myXmlTextWriter.WriteAttributeString("NameIdenti", NameIdenti);
				myXmlTextWriter.WriteAttributeString("PassIdenti", PassIdenti);
				myXmlTextWriter.WriteAttributeString("Time", Time);
				myXmlTextWriter.WriteEndDocument();
				myXmlTextWriter.Flush();
				myXmlTextWriter.Close();
				return true;
			}
			catch(Exception ex)
			{
				Console.WriteLine(ex.Message.ToString());
				return false;
			}
		}
		
		
		public Boolean LoadConfig()
		{
			try{
				XmlTextReader reader = new XmlTextReader(dirtwitter+"config.xml");
				if (reader.Read())
			    {
      				reader.MoveToContent(); 
      				//pour le moment qu'un noeud on vera plus tard si on ajoute des éléments
			      	if (reader.Name.ToString()=="Config")
 				 	{
 						NameTwitter=reader.GetAttribute("NameTwitter");
 						PassTwitter=reader.GetAttribute("PassTwitter");
 						NameIdenti=reader.GetAttribute("NameIdenti");
 						PassIdenti=reader.GetAttribute("PassIdenti");
 						Time=reader.GetAttribute("Time");
 				 		reader.Close();
 				 		return true;
 				 	}
			    }
				reader.Close();
 				return false;//on a rien trouvé	
			}
			catch
			{
				return false;
			}
		}

		
	}
}
